package com.qf.controller;

import com.qf.constants.Status;
import com.qf.dto.ContractorDto;
import com.qf.model.Contractor;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.page.Result;
import com.qf.service.ContractorService;
import com.qf.vo.ConProVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("contractor")
public class ContractorController {
    @Autowired
    private ContractorService contractorService;


    @GetMapping("findAll")
    public List<Contractor> findAll(){
        List<Contractor> contractors = contractorService.getAll();
        return contractors;
    }

    @PostMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean) {
        PageResult result = contractorService.findPage(queryPageBean);
        return result;
    }


//    @PostMapping("add")
    //urlencode方式提交参数
//    public Result addContractor(String cname, int[] pids) {
//        int row = contractorService.addAndBind(cname, pids);
//        if (row > 0) {
//            return Result.success();
//        } else {
//            return Result.fail();
//        }
//    }
   //json方式提交参数
@PostMapping("add")
   public Result addContractor(@RequestBody ConProVO conProVO) {
       int row = contractorService.addContractor(conProVO);
       if (row > 0) {
           return Result.success();
       } else {
           return Result.fail();
       }
   }


    //用来做修改时的数据回显 json
//    @GetMapping("findByCid")
//    public Result findByCid(@RequestParam("cid") String cid){
//        ContractorDto contractorDto = contractorService.findByCid(cid);
//        Result result = new Result();
//        result.setMessage("fail");
//        if (ObjectUtils.isNotEmpty(contractorDto)){
//            result.setFlag(true);
//            result.setData(contractorDto);
//            result.setMessage("success");
//        }
//        return result;
//
//    }
//}
    //联查，进行数据回显
    @GetMapping("findByCid")
    public Result findByCid(@RequestParam("cid") String cid) {
        Contractor contractor = contractorService.findByCid(cid);
        Result result = new Result();
        result.setMessage(Status.FAIL.getMessage());
        if (ObjectUtils.isNotEmpty(contractor)) {
            result.setFlag(true);
            result.setData(contractor);
            result.setMessage(Status.SUCCESS.getMessage());
        }
        return result;
    }


    @PostMapping("edit")
    public Result edit(@RequestBody ContractorDto contractorDto){
        int row  = contractorService.edit(contractorDto);
        if (row>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }


    @GetMapping("delete")
    public Result delete(@RequestParam("cid") String cid) {
        int row = contractorService.deleteByCid(cid);
        if (row > 0) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }
}